#include <iostream>
#include "mycode.h"
using namespace std;

void matrix::matrixMultiply(int **a, int **b,int **c, int rowA, int colA, int rowB, int colB)
{
	if (colA != rowA)
	{
		cout << "cols of A != rows of B\n Multiplication not possible" << endl;
		return;
	}
	//display the multipication of matrices
	for (int i = 0; i<rowA; i++){
		for (int j = 0; j<colB; j++){
			c[i][j] = 0;
			for (int k = 0; k<colA; k++){
				c[i][j] = c[i][j] + (a[i][k] * a[k][j]);
			}
		}
	}

	cout << "your matrix is" << endl;
	for (int i = 0; i<rowA; i++){
		for (int j = 0; j<colB; j++){
			cout << c[i][j] << " ";
		}
		cout << endl;
	}
}
void matrix::matrixAdd(int **a, int **b, int **sum, int rowA, int colA, int rowB, int colB)
{
	if (rowA != rowB || colA != colB)
	{
		cout << "Rows and columns of both matrices not equal.\n Addition not possible" << endl;
		return;
	}
	for (int c = 0; c < rowA; c++)
		for (int d = 0; d < colA; d++)
			sum[c][d] = a[c][d] + b[c][d];

	cout << "Sum of entered matrices:-\n";

	for (int c = 0; c < rowA; c++)
	{
		for (int d = 0; d < colA; d++)
			cout << sum[c][d] << "\t";

		cout << endl;
	}
}


void matrix::matrixSubtract(int **a, int **b, int **sum, int rowA, int colA, int rowB, int colB)
{
	if (rowA != rowB || colA != colB)
	{
		cout << "Rows and columns of both matrices not equal.\n Subtraction not possible" << endl;
		return;
	}
	for (int c = 0; c < rowA; c++)
		for (int d = 0; d < colA; d++)
			sum[c][d] = a[c][d] - b[c][d];

	cout << "Sum of entered matrices:-\n";

	for (int c = 0; c < rowA; c++)
	{
		for (int d = 0; d < colA; d++)
			cout << sum[c][d] << "\t";

		cout << endl;
	}
}
int main()
{
	int option, rowA, rowB, colA, colB, i, j;
	int **a, **b, **c;
	matrix obj;
	a = new int *[10];
	b = new int *[10];
	c = new int *[10];
	for (int i = 0; i < 10; i++)
	{
		a[i] = new int[10];
		b[i] = new int[10];
		c[i] = new int[10];
	}
	while (1)
	{
	cout << "Enter the no of rows of matrix A" << endl;
	cin >> rowA;
	cout << "Enter the no of cols of matrix A" << endl;
	cin >> colA;
	cout << "Enter the no of rows of matrix B" << endl;
	cin >> rowB;
	cout << "Enter the no of cols of matrix B" << endl;
	cin >> colB;

	cout << "\n\nEnter elements for Matrix A :::\n\n";

	for (i = 0; i<rowA; i++)
	{
		for (j = 0; j<colA; j++)
		{
			cin >> a[i][j];
		}
		cout << "\n";
	}

	cout << "\n\nMatrix A :\n\n";

	for (i = 0; i<rowA; i++)
	{
		for (j = 0; j<colA; j++)
		{
			cout << "\t" << a[i][j];
		}
		cout << "\n\n";
	}

	cout << "\n-----------------------------------------------------------\n";

	cout << "\n\nEnter elements for Matrix B :::\n\n";

	for (i = 0; i<rowB; i++)
	{
		for (j = 0; j<colB; j++)
		{
			cin >> b[i][j];
		}
		cout << "\n";
	}


	cout << "\n\nMatrix B :\n\n";

	for (i = 0; i<rowB; i++)
	{
		for (j = 0; j<colB; j++)
		{
			cout << "\t" << b[i][j];
		}
		cout << "\n\n";
	}
		cout << "Enter 1. for matrix addition" << endl;
		cout << "Enter 2. for matrix subtraction" << endl;
		cout << "Enter 3. for matrix multiplication" << endl;
		cin >> option;

		if (option == 1)
		{
		
			obj.matrixAdd(a, b, c, rowA, colA, rowB, colB);
			
		}
		else if (option == 2)
		{
			
			obj.matrixSubtract(a, b, c, rowA, colA, rowB, colB);
			
		}
		else if (option == 3)
		{
			
			obj.matrixMultiply(a, b, c, rowA, colA, rowB, colB);
			
		}
		else
		{
			cout << "Please enter the correct option" << endl;
			return -1;
		}
	}
}

